"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const log4js_1 = require("log4js");
const logger = (0, log4js_1.getLogger)('MemberProxy');
function MemberProxy(obj, intercom) {
    obj.__isProxy = true;
    return new Proxy(obj, {
        get(target, proto, receiver) {
            return Reflect.get(target, proto, receiver);
        },
        set(target, proto, value, receiver) {
            if (proto === 'state') {
                if (value !== target[proto]) {
                    const socket = intercom[target.userId];
                    const oldData = target.json();
                    const newData = { ...oldData, state: value };
                    socket === null || socket === void 0 ? void 0 : socket.emit('member:state', newData);
                    logger.debug(`member(${newData.username}):state: => ${oldData.state} -> ${newData.state} `);
                    socket === null || socket === void 0 ? void 0 : socket.broadcastMemberForAdmin(newData);
                    for (const classId of target.getClassroom()) {
                        socket === null || socket === void 0 ? void 0 : socket.broadcast.to(classId).emit('member:state', newData);
                    }
                    for (const userId of target.getAudience()) {
                        const socket = intercom[userId];
                        socket === null || socket === void 0 ? void 0 : socket.emit('member:state', newData);
                    }
                }
            }
            return Reflect.set(target, proto, value, receiver);
        }
    });
}
exports.default = MemberProxy;
